package com.cherrypt.api.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cherrypt.api.common.enums.PostInfoTypeEnum;
import com.cherrypt.api.core.dao.PostInfoDao;
import com.cherrypt.api.core.service.PostInfoService;
import com.cherrypt.api.core.util.DoubanUtils;
import com.cherrypt.api.core.util.ImdbUtils;
import com.cherrypt.api.core.util.JackJsonUtils;
import com.cherrypt.api.model.dataobject.PostInfo;
import com.cherrypt.api.model.dto.DoubanDto;
import com.cherrypt.api.model.dto.ImdbDto;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class PostInfoServiceImpl extends ServiceImpl<PostInfoDao, PostInfo> implements PostInfoService {
    @Override
    public ImdbDto getImdbByImdbId(long imdbId) {
        PostInfo postInfo = this.getOne(new QueryWrapper<PostInfo>()
                .eq("info_id", imdbId)
                .eq("type", PostInfoTypeEnum.IMDB.getType()));
        if (postInfo == null) {
            return null;
        }
        try {
            JsonNode rootNode = JackJsonUtils.parse(postInfo.getContent());
            return ImdbUtils.getImdb(rootNode);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public ImdbDto getByImdbId(long imdbId) {
        PostInfo postInfo = this.baseMapper.getByInfoId(imdbId);
        if (postInfo == null){
            return null;
        }
        try {
            JsonNode rootNode = JackJsonUtils.parse(postInfo.getContent());
            return ImdbUtils.getImdb(rootNode);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public ImdbDto getPlotByImdbId(long imdbId) {
        PostInfo postInfo = this.baseMapper.getSummaryByInfoId(imdbId);
        if (postInfo == null) {
            return null;
        }
        ImdbDto imdbDto = new ImdbDto();
        imdbDto.setImdbId(imdbId);
        imdbDto.setPlot(postInfo.getSummary());
        imdbDto.setPoster(postInfo.getImage());
        imdbDto.setImdbRating(postInfo.getRatingAverage());
        imdbDto.setImdbVotes(postInfo.getRatingNumRaters());
        return imdbDto;
    }

    @Override
    public DoubanDto getDoubanByDoubanId(long doubanId, PostInfoTypeEnum postInfoTypeEnum) {
        PostInfo postInfo = this.getOne(new QueryWrapper<PostInfo>()
                .eq("info_id", doubanId)
                .eq("type", postInfoTypeEnum.getType()));
        if (postInfo == null) {
            return null;
        }
        try {
            JsonNode rootNode = JackJsonUtils.parse(postInfo.getContent());
            return DoubanUtils.getDouban(rootNode);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public DoubanDto getSummaryByDoubanId(long doubanId) {
        PostInfo postInfo = this.baseMapper.getSummaryByInfoId(doubanId);
        if (postInfo == null) {
            return null;
        }
        DoubanDto doubanDto = new DoubanDto();
        doubanDto.setDoubanId(doubanId);
        doubanDto.setSummary(postInfo.getSummary());
        doubanDto.setImage(postInfo.getImage());
        doubanDto.setRatingAverage(postInfo.getRatingAverage());
        doubanDto.setRatingNumRaters(postInfo.getRatingNumRaters());
        return doubanDto;
    }

    @Override
    public DoubanDto getByDoubanId(long doubanId) {
        PostInfo postInfo = this.baseMapper.getByInfoId(doubanId);
        if (postInfo == null){
            return null;
        }
        try {
            JsonNode rootNode = JackJsonUtils.parse(postInfo.getContent());
            return DoubanUtils.getDouban(rootNode);
        } catch (IOException e) {
            return null;
        }
    }
}
